var util = require('../../libs/util.js');
var moment = require('../../libs/moment.min.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasData: false,
    monthly_start:'',
    monthly_end:''
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": "车辆档案" });
    var that = this;
    wx.showLoading({
      title: '请稍候...',
    });
    //更新数据
    that.setData({
      vehicle_id: e.id,
      tab: e.tab || 0,
      monthly_start:moment().format('YYYY-MM'),
      monthly_end:moment().format('YYYY-MM'),
      role_default:app.globalData.role_default
    });
    that.loadData()
  },
  loadData:function(){
    var that = this;
    util.getData("/wechat/vehicle/vehicleMonthlyLoan", { vehicle_id: that.data.vehicle_id }, function (res) {
      console.log(res.data.data)
      if (res.data.success) {
        wx.setNavigationBarTitle({ "title": "车辆档案--" + res.data.data.plate });
        if(res.data.data.ID){
          that.setData(res.data.data)
        }
      }
      wx.hideLoading();
    });
  },
  submitForms: function (e) {
    var that = this;
    if (that.data.loan_total == '' || isNaN(that.data.loan_total) == true) {
      wx.showToast({
        title: "请检查总贷款额",
        image: '/images/wrong.png',
        duration: 2000
      });
      return false;
    }
    if (that.data.loan_payed == '' || isNaN(that.data.loan_payed) == true) {
      wx.showToast({
        title: "请检查已交金额",
        image: '/images/wrong.png',
        duration: 2000
      });
      return false;
    }
    if (that.data.monthly_number == '' || isNaN(that.data.monthly_number) == true) {
      wx.showToast({
        title: "请检查月供期数",
        image: '/images/wrong.png',
        duration: 2000
      });
      return false;
    }
    if (that.data.monthly_pay == '' || isNaN(that.data.monthly_pay) == true) {
      wx.showToast({
        title: "请检查月供金额",
        image: '/images/wrong.png',
        duration: 2000
      });
      return false;
    }
    wx.showLoading({
      title: '请稍候...',
    });
    if (that.data.ID){
      util.putData("/wechat/vehicle/vehicleMonthlyLoan", {
        id: that.data.ID,
        loan_total: that.data.loan_total,
        loan_payed: that.data.loan_payed,
        monthly_number: that.data.monthly_number,
        monthly_pay: that.data.monthly_pay,
        monthly_start: that.data.monthly_start,
        monthly_end: that.data.monthly_end
      }, function (res, error) {
        console.log(res); console.log(error)
        wx.hideLoading();
        if (error) {
          wx.showToast({
            title: res.data.data,
            image: '/images/wrong.png',
            duration: 3000
          });
          return false;
        }
        if (res.data.success) {
          wx.showToast({
            title: res.data.data,
            icon: "success",
            duration: 2000,
            success:function(){
              setTimeout(function(){
                that.loadData();
              },2000)
            }
          });
        } else if (res.data.success == false) {
          wx.showToast({
            title: res.data.data,
            image: '/images/wrong.png',
            duration: 3000,
            mask: true
          });
        }
      });
    }else{
      util.postData("/wechat/vehicle/vehicleMonthlyLoan", {
        vehicle:that.data.vehicle_id,
        loan_total: that.data.loan_total,
        loan_payed: that.data.loan_payed,
        monthly_number: that.data.monthly_number,
        monthly_pay: that.data.monthly_pay,
        monthly_start: that.data.monthly_start,
        monthly_end: that.data.monthly_end
      }, function (res, error) {
        console.log(res); console.log(error)
        wx.hideLoading();
        if (error) {
          wx.showToast({
            title: res.data.data,
            image: '/images/wrong.png',
            duration: 3000
          });
          return false;
        }
        if (res.data.success) {
          wx.showToast({
            title: res.data.data,
            icon: "success",
            duration: 2000,
            success: function () {
              setTimeout(function () {
                that.loadData();
              }, 2000)
            }
          });
        } else if (res.data.success == false) {
          wx.showToast({
            title: res.data.data,
            image: '/images/wrong.png',
            duration: 3000,
            mask: true
          });
        }
      });
    }
    
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);

    if(item == 'monthly_number'){
      this.setData({
        monthly_end:moment().add(e.detail.value,'month').format('YYYY-MM')
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})